// 2013-10-31上午08:34:01  net.betou.core.service.post.impl.SmsSenderImpl.java

package net.betou.core.service.post.impl;

import net.betou.common.security.encoder.PwdEncoder;
import net.betou.common.util.DataConvertUtils;
import net.betou.core.service.post.SmsSender;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * 发送GET请求，获取短信剩余数量
 * 
 * @author 叶装装
 */
public class SmsSenderImpl implements SmsSender{

	public Integer getSmsNum( ) {

		DefaultHttpClient httpClient = new DefaultHttpClient( );

		HttpGet httpGet = new HttpGet( getUrl( ) );

		HttpResponse response;

		try{
			response = httpClient.execute( httpGet );
			HttpEntity entity = response.getEntity( );// HttpClient返回的信息均以entity保存
			
			String [] entityString = StringUtils.split(  EntityUtils.toString(entity, "UTF-8"), "||" );
			
			EntityUtils.consume( entity );
			
			return DataConvertUtils.getInt( entityString[ 1 ] );
			
		}catch( Exception e ){
			e.printStackTrace( );
		}finally{
			httpClient.getConnectionManager( ).shutdown( );
		}

		return 0;
	}

	private String getUrl( ) {
		return supplusHost + "?uid=" + smsUsername + "&pwd=" + smsPwdEncoder.encodePassword( smsPassword );
	}

	public void setSupplusHost( String supplusHost ) {
		this.supplusHost = supplusHost;
	}

	public void setSmsUsername( String smsUsername ) {
		this.smsUsername = smsUsername;
	}

	public void setSmsPassword( String smsPassword ) {
		this.smsPassword = smsPassword;
	}

	public void setSmsPwdEncoder( PwdEncoder smsPwdEncoder ) {
		this.smsPwdEncoder = smsPwdEncoder;
	}

	private String supplusHost;
	private String smsUsername;
	private String smsPassword;
	private PwdEncoder smsPwdEncoder; // 短信密码加密类
}
